package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.entity.ParamInfo;
import eteam.aps.model.vo.ParamFromWindowingVo;
import eteam.aps.model.vo.ParamInfoDtlVo;
import eteam.aps.model.vo.ParamInfoVo;
import eteam.aps.service.ParamFromUserService;
import eteam.aps.service.ParamInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 参数信息控制层
 *
 * @author WMT
 */
@Api(tags = "参数信息模块")
@RestController
public class ParamInfoController implements IParamInfoController{

    @Autowired
    private ParamInfoService paramInfoService;

    @Autowired
    private ParamFromUserService paramFromUserService;

    @Override
    @ApiOperation("新增参数")
    @PostMapping("/dp/insert")
    public R<Integer> insert(@RequestBody ParamInfo paramInfo) throws Exception, AuthException {
        if (Helper.isEmpty(paramInfo.getfParamCode()) || Helper.isEmpty(paramInfo.getfParamName())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数编号或参数名称不能为空");
        }
        if (Helper.isEmpty(paramInfo.getfParamValueID())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数值ID不能为空");
        }
        if (Helper.isEmpty(paramInfo.getfParamValue())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数值不能为空");
        }
        if (paramInfoService.selectCountByFDpCode(paramInfo.getfParamCode()) > 0){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数编号已存在");
        }
        if (paramInfoService.selectCountByFDpName(paramInfo.getfParamName()) > 0){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数名称已存在");
        }
        return R.success(paramInfoService.insert(paramInfo));
    }

    @Override
    @ApiOperation("删除参数")
    @PostMapping("/dp/delete/{fId}")
    public R<Integer> delete(@PathVariable String fId) throws Exception, AuthException {
        if (Helper.isEmpty(fId)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"没有参数内码");
        }
        ParamInfoVo paramInfoDb = paramInfoService.selectByfId(fId);
        if (paramInfoDb == null){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数不存在");
        }
        if (paramInfoService.selectFStateByFId(fId) == ParentEntity.FSTATE_DELETED){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数已被删除");
        }
        return R.success(paramInfoService.delete(fId));
    }

    @Override
    @ApiOperation("更新参数")
    @PostMapping("/dp/update")
    public R<Integer> update(@RequestBody ParamInfo paramInfo) throws Exception, AuthException {
        if (Helper.isEmpty(paramInfo.getfParamCode()) || Helper.isEmpty(paramInfo.getfParamName())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数编号或参数名称不能为空");
        }
        if(Helper.isEmpty(paramInfo.getfId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"fId不能为空");
        }
        if (Helper.isEmpty(paramInfo.getfParamValueID())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数值ID不能为空");
        }
        if (Helper.isEmpty(paramInfo.getfParamValue())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"参数值不能为空");
        }
        ParamInfoVo dbParam = paramInfoService.selectByfId(paramInfo.getfId());
        if (!paramInfo.getfParamCode().equals(dbParam.getfParamCode())){
            if (paramInfoService.selectCountByFDpCode(paramInfo.getfParamCode()) > 0){
                throw new BusinessException(HttpStatus.BAD_REQUEST,"参数编号已存在");
            }
        }
        if (!dbParam.getfParamName().equals(paramInfo.getfParamName())){
            if (paramInfoService.selectCountByFDpName(paramInfo.getfParamName()) > 0){
                throw new BusinessException(HttpStatus.BAD_REQUEST,"参数名称已存在");
            }
        }
        return R.success(paramInfoService.update(paramInfo));
    }


    @Override
    @ApiOperation("分页查询参数")
    @PostMapping("/dp/selectAll")
    public R<JsonPage<ParamInfoVo>> selectAll(
            @RequestParam(value = "fParamCode",required = false) String fParamCode,
            @RequestParam(value = "fParamName", required = false) String fParamName,
            @RequestParam("page") Integer page,
            @RequestParam("pageSize") Integer pageSize
    ) throws Exception, AuthException {

        HashMap<String, Object> map = new HashMap<>(4);
        if (Helper.isNotEmpty(fParamCode)){
            map.put("fParamCode",Helper.likeSql(fParamCode));
        }
        if (Helper.isNotEmpty(fParamName)){
            map.put("fParamName",Helper.likeSql(fParamName));
        }
        return R.success(paramInfoService.selectAll(
                map,Helper.valuePositive(page,1),Helper.valuePositive(pageSize,10))
        );
    }

    @Override
    @ApiOperation("查询参数详情")
    @PostMapping("/dp/selectDetails/{fId}")
    public R<ParamInfoVo> selectDetails(@PathVariable String fId) throws Exception, AuthException {
        ParamInfoVo paramInfoDb = paramInfoService.selectByfId(fId);
        if (paramInfoDb == null){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"fId不存在");
        }
        if (Helper.isEmpty(fId)){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"fId不能为空");
        }
        return R.success(paramInfoService.selectByfId(fId));
    }

    @Override
    @ApiOperation("参数来源开窗")
    @PostMapping("/dp/selectFw/{paramFrom}")
    public R<List<ParamFromWindowingVo>> selectFromWindowing(@PathVariable Integer paramFrom) throws Exception, AuthException {

        if (paramFrom == ParamInfo.FPARAMFROM_DICTIONARY){
            return R.success(paramInfoService.selectPfromDdc());
        }
        if (paramFrom == ParamInfo.FPARAMFROM_USER){
            return R.success(paramFromUserService.selectPfromSu());
        }
        if (paramFrom == ParamInfo.FPARAMFROM_ORG){
            return R.success(paramInfoService.selectPfromSo());
        }
        return R.success(null);
    }

    @Override
    @ApiOperation("产品参数子表查询")
    @PostMapping("/dp/selectParamDtl")
    public R<List<ParamInfoDtlVo>> selectParamDtl() throws Exception, AuthException {
        return R.success(paramInfoService.selectParamDtl());
    }


}
